package com.hotel.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hotel.entity.RoomInventory;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

@Mapper
public interface RoomInventoryMapper extends BaseMapper<RoomInventory> {

    /**
     * 根据日期和房型id查询库存
     * @param date
     * @param roomTypeId
     * @return
     */
    @Select("select * from room_inventory where inventory_date = #{date} and room_type_id = #{roomTypeId}")
    RoomInventory getByDateAndRoomTypeId(LocalDateTime date, Integer roomTypeId);

    /**
     * 添加一间房间
     * @param roomTypeId
     */
    @Update("update room_inventory set total_rooms = total_rooms + 1, available_rooms = available_rooms + 1 where room_type_id = #{roomTypeId}")
    void addOneRoom(Integer roomTypeId);

    /**
     * 删除一间房间
     * @param roomTypeId
     */
    @Update("update room_inventory set total_rooms = total_rooms - 1, available_rooms = available_rooms - 1 where room_type_id = #{roomTypeId}")
    void deleteOneRoom(Integer roomTypeId);

    /**
     * 添加房间类型
     * @param roomTypeId
     */
    void addRoomType(Integer roomTypeId,LocalDateTime inventoryDate);

    /**
     * 删除房间类型
     * @param roomTypeId
     */
    @Delete("delete from room_inventory where room_type_id = #{roomTypeId}")
    void deleteRoomType(Integer roomTypeId);

    /**
     * 预订或入住
     * @param roomTypeId
     * @param beginDate
     * @param endDate
     */
    @Update("update room_inventory set available_rooms = available_rooms - 1,booked_rooms = booked_rooms + 1 where room_type_id = #{roomTypeId} and inventory_date between #{beginDate} and #{endDate}")
    void bookOrCheckIn(Integer roomTypeId, LocalDateTime beginDate, LocalDateTime endDate);

    /**
     * 取消预订
     * @param roomTypeId
     * @param beginDate
     * @param endDate
     */
    @Update("update room_inventory set available_rooms = available_rooms + 1,booked_rooms = booked_rooms - 1 where room_type_id = #{roomTypeId} and inventory_date between #{beginDate} and #{endDate}")
    void cancelBookOrCheckOut(Integer roomTypeId, LocalDateTime beginDate, LocalDateTime endDate);
}
